﻿#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <strsafe.h>
#include <stdio.h>
HINSTANCE hInst;
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow
)
{
	hInst = hInstance;
	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.lpszClassName = "MainClass";
	wc.lpszMenuName = nullptr;
	wc.hCursor = (HCURSOR)LoadCursor(0, IDC_ARROW);
	wc.hIcon = (HICON)LoadIcon(0, IDI_APPLICATION);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = MainWindowProc;
	wc.hInstance = hInstance;
	if(!RegisterClass(&wc))MessageBox(nullptr, "ClassError", "Error", MB_ICONHAND);
	HWND hWnd = CreateWindow("MainClass", "MainWindow", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);
	if (!hWnd)MessageBox(nullptr, "hWndError", "Error", MB_ICONHAND);
	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);
	MSG msg;
	while (1)
	{
		GetMessage(&msg, nullptr, 0, 0);
		if (msg.message == WM_QUIT)break;
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static HDC hdc;
	static HWND btHwnd[1];
	static HWND editHwnd[2];
	static TEXTMETRIC tm;
	static int cxChar, cyChar;
	switch (uMsg)
	{
	case WM_CREATE:
		//system("kakasi-2.3.6\\src\\.libs\\kakasi.exe -JH <input.txt>output.txt -i utf8 -o utf8");
		hdc = GetDC(hWnd);
		GetTextMetrics(hdc, &tm);
		cxChar = tm.tmAveCharWidth;
		cyChar = tm.tmHeight + tm.tmExternalLeading;
		ReleaseDC(hWnd, hdc);
		btHwnd[0] = CreateWindow("Button", "确定", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, cxChar*14+10, 0, cxChar*4+10, cyChar+10, hWnd, nullptr, hInst, nullptr);
		editHwnd[0] = CreateWindow("Edit", "输入", WS_CHILD | WS_VISIBLE | WS_BORDER, 0, cyChar*2+10, cxChar*40+10, cyChar*40+10, hWnd, (HMENU)114514, hInst, nullptr);
		editHwnd[1] = CreateWindow("Edit", "输出", WS_CHILD | WS_VISIBLE | WS_BORDER, cxChar * 45 + 10, cyChar*2+10, cxChar*40+10, cyChar*40+10, hWnd, (HMENU)1919, hInst, nullptr);
		
		break;
	case WM_COMMAND:
		if ((HWND)lParam == btHwnd[0])
		{
			char buffer[1001];
			GetDlgItemText(hWnd, 1919, buffer, 1000);
			FILE* fp = fopen("input.txt", "w+");
			fputs(buffer,fp);
			fclose(fp);
			system("kakasi-2.3.6\\src\\.libs\\kakasi.exe -JH <input.txt>output.txt -i gbk -o gbk");
			SetDlgItemText(hWnd, 114514, "fuck");
		}
		break;
	case WM_PAINT:
		hdc = GetDC(hWnd);
		TextOut(hdc, 0, 0, "请输入转换文本", 7);

		ReleaseDC(hWnd, hdc);
		break;
	case WM_CLOSE:
		DestroyWindow(hWnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}